package com.xiaomei.manager.web.expand;

import java.util.Properties;
import java.util.Random;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import com.xiaomei.manager.utils.DESUtils;

/**
* 重写PropertyPlaceholderConfigurer的processProperties方法实现
*
* @author leizhimin 2012-03-14 16:47
*/
public class PropertyPlaceholderConfigurerExt extends PropertyPlaceholderConfigurer{

	private static final String key = "9694358131015327";

	protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props)
		throws BeansException {
		try {
			processJdbc(props);
			
//			processSTS(props);
			
			super.processProperties(beanFactory, props);
		} catch (Exception e) {
			e.printStackTrace();
			throw new BeanInitializationException(e.getMessage());
		}
	}

	private void processJdbc(Properties props) {
		String username = props.getProperty("jdbc.username");
		if (username != null) {
			props.setProperty("jdbc.username", DESUtils.Decrypt(username, DESUtils.hex2byte(key)));
		}
		
		String password = props.getProperty("jdbc.password");
		if (password != null) {
			props.setProperty("jdbc.password", DESUtils.Decrypt(password, DESUtils.hex2byte(key)));
		}
		
		String url = props.getProperty("jdbc.url");
		if (url != null) {
			props.setProperty("jdbc.url", DESUtils.Decrypt(url, DESUtils.hex2byte(key)));
		}
	}
	
	private void processSTS(Properties props) {
		String region = props.getProperty("region");
		if (region != null) {
			props.setProperty("region", DESUtils.Decrypt(region, DESUtils.hex2byte(key)));
		}
		
		String sts_api_version = props.getProperty("sts_api_version");
		if (sts_api_version != null) {
			props.setProperty("sts_api_version", DESUtils.Decrypt(sts_api_version, DESUtils.hex2byte(key)));
		}
		
		String AccessKeyID = props.getProperty("AccessKeyID");
		if (AccessKeyID != null) {
			props.setProperty("AccessKeyID", DESUtils.Decrypt(AccessKeyID, DESUtils.hex2byte(key)));
		}
		
		String AccessKeySecret = props.getProperty("AccessKeySecret");
		if (AccessKeySecret != null) {
			props.setProperty("AccessKeySecret", DESUtils.Decrypt(AccessKeySecret, DESUtils.hex2byte(key)));
		}
		
		String RoleArn = props.getProperty("RoleArn");
		if (RoleArn != null) {
			props.setProperty("RoleArn", DESUtils.Decrypt(RoleArn, DESUtils.hex2byte(key)));
		}
		
		String TokenExpireTime = props.getProperty("TokenExpireTime");
		if (TokenExpireTime != null) {
			props.setProperty("TokenExpireTime", DESUtils.Decrypt(TokenExpireTime, DESUtils.hex2byte(key)));
		}
		
		String PolicyFile = props.getProperty("PolicyFile");
		if (PolicyFile != null) {
			props.setProperty("PolicyFile", DESUtils.Decrypt(PolicyFile, DESUtils.hex2byte(key)));
		}
	}
	
	public static void main(String[] args) {
		Random r = new Random();
		for(int i=0 ;i<16; i++) {
			System.out.print(r.nextInt(10));
		}
	}
}
